/**
 * OSAmI: Open Source Ambient Intelligence
 * Copyright (C) 2010 Bull S.A.S.
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 *
 * --------------------------------------------------------------------------
 * $Id: DefaultUriAnalyzer.java 377 2011-04-20 08:28:36Z osami.fr $
 * --------------------------------------------------------------------------
 */


package org.osami.gateway.display.internal.uri.internal;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.osami.gateway.display.internal.uri.UriAnalyzer;

/**
 * A {@code DefaultUriAnalyzer} is ...
 *
 * @author Guillaume Sauthier
 */
public class DefaultUriAnalyzer implements UriAnalyzer {

    private String info;
    private Pattern pattern;
    private Map<Integer, String> indexes;

    public DefaultUriAnalyzer(String pattern) {
        this.info = pattern;
        buildRegexpPattern();
    }

    private void buildRegexpPattern() {
        this.indexes = new HashMap<Integer, String>();
        StringBuilder compilable = new StringBuilder();
        String parsed = this.info;
        int groupId = 1;
        StringBuilder name = new StringBuilder();
        boolean inTemplate = false;

        while (parsed.length() != 0) {
            char current = parsed.charAt(0);
            parsed = parsed.substring(1);

            if (current == '{') {
                inTemplate = true;
            } else if (inTemplate && (current == '}')) {
                inTemplate = false;
                compilable.append("(.*)");
                this.indexes.put(groupId++, name.toString());
                name = new StringBuilder();
            } else {
                if (inTemplate) {
                    name.append(current);
                } else {
                    compilable.append(current);
                }
            }

        }

        this.pattern = Pattern.compile(compilable.toString());
    }

    public Map<String, String> analyze(String path) {
        if (path == null) {
            path = ""; // Avoid a NPE.
        }
        Matcher matcher = this.pattern.matcher(path);
        if (!matcher.matches()) {
            return null;
        }

        Map<String, String> result = new HashMap<String, String>();

        int count = matcher.groupCount();
        for (int i = 1; i <= count; i++) {
            String group = matcher.group(i);
            result.put(this.indexes.get(i), group);
        }

        return result;
    }

    public String getPattern() {
        return this.info;
    }

    @Override
    public String toString() {
        return "DefaultUriAnalyzer[" + this.info + "]";
    }

    @Override
    public int hashCode() {
        return this.info.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }

        if (obj instanceof DefaultUriAnalyzer) {
            DefaultUriAnalyzer other = (DefaultUriAnalyzer) obj;
            return this.info.equals(other.info);
        }
        return false;
    }
}
